package com.benmei.weike.service;

import com.benmei.weike.dao.TsNtSendHisDao;
import com.benmei.weike.entity.TsNtSendHis;
import com.benmei.weike.util.CommonUtil;
import com.benmei.weike.util.SMSUtil;
import com.benmei.weike.util.Sms;
import com.benmei.weike.util.SmsCodeUtil;
import com.nativetalk.base.RetInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;

/**
 * Created by Peter on 2018/11/10.
 * 短信服务
 */
@Service
public class SMSService {
    private static Logger logger = LoggerFactory.getLogger(SMSService.class);

    @Resource
    private TsNtSendHisDao tsNtSendHisDao;

    @Transactional
    public RetInfo sendMessage(Sms sms) {
        String logInfo = this.getClass().getName() + ":smsSendMessage:";
        logger.info("=================" + logInfo + "====================");

        RetInfo retInfo = RetInfo.getSuccessInfo();

        try {
            String content = "您的验证码为CODE（5分钟内有效，如非本人操作请忽略）";
            int code = Integer.parseInt(SmsCodeUtil.getSmsCode());
            content = content.replace("CODE", code + "");
            logger.info(content);

            Integer count = tsNtSendHisDao.codeCountByPhone(sms.getMemb_phone());


            //今天是否发送过短信，如果发送过短信就校验是否超过20条
            if (count > 20) {
                retInfo.setMark(RetInfo.ReturnCode.CLIENT_ERROR);
                retInfo.setTip("发送短信超过20条");
                retInfo.setObj(sms.getMemb_phone());
                logger.info("发送短信超过20条" + sms.getMemb_phone());
                return retInfo;
            }

            //检查60秒是否发送过短信
            Timestamp now = new Timestamp(System.currentTimeMillis());
            TsNtSendHis tsh = tsNtSendHisDao.lastCodeByPhone(sms.getMemb_phone());
            if (tsh != null) {
                Timestamp time = tsh.getSend_time();
                logger.info("now=" + now + ", lastTime=" + time + ",result = " + (now.getTime() - time.getTime()) / 1000 + "秒");
                if ((now.getTime() - time.getTime()) < 50 * 1000) {
                    retInfo.setMark(RetInfo.ReturnCode.CLIENT_ERROR);
                    retInfo.setTip("60秒内发送过短信，过于频繁");
                    retInfo.setObj(sms.getMemb_phone());
                    logger.info("60秒内发送过短信，过于频繁" + sms.getMemb_phone());
                    return retInfo;
                }
            }

            //在数据库中插入该记录
            TsNtSendHis tsNtSendHis = new TsNtSendHis();
            tsNtSendHis.setPhoneno(sms.getMemb_phone());
            tsNtSendHis.setSms_provider_id(code);
            tsNtSendHis.setYyyymmdd(CommonUtil.getMonth(now));
            tsNtSendHis.setSms_type("Y");
            tsNtSendHis.setSend_time(now);
            tsNtSendHisDao.insertCode(tsNtSendHis);

            //发送短信
            boolean flag = SMSUtil.sendSms(sms, content);
            retInfo.setMark(RetInfo.ReturnCode.SUCCESS);
            retInfo.setTip("短信发送成功");
            retInfo.setObj(sms);
            return retInfo;
        } catch (Exception e) {
            retInfo.setMark(RetInfo.ReturnCode.CLIENT_ERROR);
            retInfo.setTip("系统错误");
            logger.error(e.getMessage(), e);
            return retInfo;
        }
    }


}
